Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build es modules with .mjs extension #573

Merged
merged 1 commit into from
Nov 12, 2022
Merged

Conversation

strickczq
Copy link
Contributor

get rid of SyntaxError: Cannot use import statement outside a module

@vercel
Copy link

vercel bot commented Sep 13, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
giscus-component ✅ Ready (Inspect) Visit Preview Nov 12, 2022 at 5:37PM (UTC)

@strickczq strickczq changed the title build es modules with .mjs extention build es modules with .mjs extension Sep 13, 2022
@laymonage
Copy link
Member

Could you provide more details on how you got the error, and why this would fix it? I'm not very familiar with what the state of the art for handling mjs/cjs should be like, so I'm afraid if this might fix a particular setup but break others.

@strickczq
Copy link
Contributor Author

I use vite-ssg to generate static website, the build script is run as es module in node environment, like what run.mjs in my following tests does (using an import syntax)

To reproduce

source file: giscus-test.zip

image

To fix

source file: giscus-test-patched.zip

image

What's the problem

import syntax is only available when:

  • setting type: "module" in package.json, then all .js files are considered as es modules
  • otherwise, only .mjs files are considered as es modules

As a library, giscus-component should support both esm and cjs, then setting type: "module" is impossible.

Distinguish the output js files with .mjs and .cjs would be the best option.

Fix `SyntaxError: Cannot use import statement outside a module`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants